#include <head.h>

int main(int argc, char const *argv[])
{
    int pid = 0;
    int wait_read = 0;
    if (-1 == (pid = fork()))
    {
        perror("fork error");
        return 0;
    }

    // 父进程
    else if (0 < pid)
    {
        printf("I'm parent process, my pid is %d\n", getpid());
        sleep(1);
    }

    // 子进程
    else
    {
        printf("I'm child process, my pid is %d\n", getpid());
        sleep(30);
        printf("child process ended\n");
        exit(-1);
    }

    wait(&wait_read);
    if (WIFEXITED(wait_read)) // 判断 状态码是否正常退出
    {
        printf("child process exited, status = %d\n", WEXITSTATUS(wait_read)); // 正常退出exit(10)
    }
    else if (WIFSIGNALED(wait_read))
    {
        printf("child process terminated by signal %d\n", WTERMSIG(wait_read)); // 被信号中断 kill -9 pid
    }
    else if (WIFSTOPPED(wait_read))
    {
        printf("child process stopped by signal %d\n", WSTOPSIG(wait_read)); // 被信号暂停 kill -19 pid
    }
    
    printf("zhangsan");
    wait(&wait_read);
    if (WIFCONTINUED(wait_read))
    {
        printf("child process started by signal"); // 信号回复 kill -18 pid
    }

    while (1)
    {
        sleep(1);
    }

    return 0;
}
